package com.example.class_manage.Controller;

import com.example.class_manage.Entity.Homework_text;
import com.example.class_manage.Entity.Howework_pic;
import com.example.class_manage.Entity.question_pic;
import com.example.class_manage.Entity.question_text;
import com.example.class_manage.Mapper.All_HomeworkQuestion;
import com.example.class_manage.Mapper.HomeWork_pic_mapper;
import com.example.class_manage.Mapper.HomeWork_text_mapper;
import com.example.class_manage.Util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Songxiao
 * @create 2021-11-04 22:59
 */
@RequestMapping("/Student")
@RestController
@Api(tags = "学生作业接口")
public class Student_Homework_Controller {
    @Autowired
    HomeWork_pic_mapper homeWork_pic_mapper;
    @Autowired
    HomeWork_text_mapper homeWork_text_mapper;
    @Autowired
    All_HomeworkQuestion all_homeworkQuestion;
    @ApiOperation("查询特定学生的照片作业")
    @GetMapping("/Find_AllphotoHomework")//查询特定学生的照片作业
    public JsonData Find_AllphotoHomeword(@ApiParam(value = "作业学生id", required = true) @RequestParam String stu_id) {
        List<Howework_pic> find = homeWork_pic_mapper.Find_All_bystu_id_photoHomework(stu_id);
        System.out.println(find.size());
        return JsonData.buildSuccess(find, "查询此学生所有作业成功");
    }
    @ApiOperation("查询特定学生的文字作业")
    @GetMapping("/Find_AlltextHomework")//查询特定学生的文字作业
    public JsonData Find_AlltextHomeword(@ApiParam(value = "作业学生id", required = true) @RequestParam String stu_id) {
        List<Homework_text> find = homeWork_text_mapper.Find_All_bystu_id_textHomework(stu_id);
        System.out.println(find.size());
        return JsonData.buildSuccess(find, "查询此学生所有作业成功");
    }

/*    @GetMapping("/test")
    public String find()
    {
        List<Howework_pic> howework_pics = homeWork_pic_mapper.Find_AllHomework();
        System.out.println(howework_pics.size());
        BASE64Encoder encoder = new BASE64Encoder();
        byte[] data = howework_pics.get(1).getHomework_img();
        String target = encoder.encode(data);
        return target;
    }*/
    @ApiOperation("获得此学生所有文字作业")
    @GetMapping("/Review_All_text_bystu_id")
    public JsonData Review_ALL_text(@RequestParam String stu_id)
    {
        List<Homework_text> find = homeWork_text_mapper.Review_All_text_bystu_id(stu_id);
        System.out.println(find.size());
        return JsonData.buildSuccess(find, "查询此学生所有作业成功");
    }
    @ApiOperation("获得此学生所有照片作业")
    @GetMapping("/Review_All_pic_bystu_id")
    public JsonData Review_ALL_pic(@RequestParam String stu_id)
    {
        List<Howework_pic> ans = homeWork_pic_mapper.Review_All_pic_bystu_id(stu_id);
        return ans.size()!=0?JsonData.buildSuccess(ans,"查询成功"):JsonData.buildError("没数据");
    }

    @ApiOperation("获得此学生收藏的图片作业")
    @GetMapping("/Review_pic_byFlag")
    public JsonData Review_pic_byFlag(@RequestParam String stu_id)
    {
        List<Howework_pic> ans = homeWork_pic_mapper.Review_pic_byFlag(stu_id);
        return ans.size()!=0?JsonData.buildSuccess(ans,"查询成功"):JsonData.buildError("没数据");
    }
    @ApiOperation("获得学生收藏的文字作业")
    @GetMapping("/Review_text_byFlag")
    public JsonData Homework_text_byFlag(@ApiParam(value = "学生id",required = true)@RequestParam String stu_id){
        List<Homework_text> ans = homeWork_text_mapper.Review_text_byFlag(stu_id);
        return ans.size()!=0?JsonData.buildSuccess(ans,"查询成功"):JsonData.buildError("没数据");
    }

    @ApiOperation("学生提交照片作业")
    @PostMapping("/Insert_onePhoto")
    public JsonData Insert_onerecord(@ApiParam(value = "作业号",required = true) @RequestParam String id,
                                     @ApiParam(value = "作业照片",required = true) @RequestParam MultipartFile file,
                                     @ApiParam(value = "作业学生姓名",required = true) @RequestParam String student_name,
                                     @ApiParam(value = "作业学生id",required = true) @RequestParam String stu_id,
                                     @ApiParam(value = "作业是否收藏",required = true) @RequestParam String Flag
    ) {
        Integer statu = 0;
        Date date = new Date();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");//标准化时间
        try {
            byte[] data;
            data = file.getBytes();
            statu = homeWork_pic_mapper.Insert_onerecord(id,student_name,data,sdf.format(date),stu_id,Flag);
            //uuid是作业号
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (statu != 0) {
                all_homeworkQuestion.UpdateSubmit_pic(stu_id, id);
            }
            return statu != 0 ? JsonData.buildSuccess("插入成功") : JsonData.buildError("插入失败文件太大");
        }
    }

    @ApiOperation("学生提交文字作业")
    @PostMapping("/Insert_oneText")
    public JsonData Insert_onerecord(@ApiParam(value = "作业号", required = true) @RequestParam String id,
                                     @ApiParam(value = "作业学生姓名", required = true) @RequestParam String student_name,
                                     @ApiParam(value = "作业答案") @RequestParam String Homework_text,
                                     @ApiParam(value = "作业学生id", required = true) @RequestParam String stu_id,
                                     @ApiParam(value = "作业是否收藏", required = true) @RequestParam String Flag
    ) {
        Integer statu = 0;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//标准化时间
        statu = homeWork_text_mapper.Insert_onerecordtext(id, student_name, sdf.format(date), stu_id, Flag, Homework_text);
        if (statu != 0) {
            all_homeworkQuestion.UpdateSubmit_text(stu_id, id);
        }
        return statu != 0 ? JsonData.buildSuccess("插入成功") : JsonData.buildError("插入失败");

    }

    @ApiOperation("收藏图片作业")
    @PostMapping("Update_picflag")
    public JsonData Updatepic_flag(
            @ApiParam(value = "是否收藏", required = true) @RequestParam String flag,
            @ApiParam(value = "学生id", required = true) @RequestParam String stu_id,
            @ApiParam(value = "作业id", required = true) @RequestParam String id
    ) {
        Integer statu = 0;
        statu = homeWork_pic_mapper.Update_flag(flag, stu_id, id);
        return statu != 0 ? JsonData.buildSuccess("更改成功") : JsonData.buildError("更改失败");
    }

    @ApiOperation("收藏文字作业")
    @PostMapping("Update_textflag")
    public JsonData Update_textflag(
            @ApiParam(value = "是否收藏", required = true) @RequestParam String flag,
            @ApiParam(value = "学生id", required = true) @RequestParam String stu_id,
            @ApiParam(value = "作业id", required = true) @RequestParam String id
    ) {
        Integer statu = 0;
        statu = homeWork_text_mapper.Update_flag(flag, stu_id, id);
        return statu != 0 ? JsonData.buildSuccess("更改成功") : JsonData.buildError("更改失败");
    }



    @ApiOperation("学生查询自己需要提交的照片作业")
    @GetMapping("/Question_pic")
    public JsonData Question_pic(@RequestParam("stu_id") String stu_id) {
        List<question_pic> target = all_homeworkQuestion.Find_Question_pic(stu_id);
        return target.size() != 0 ? JsonData.buildSuccess(target, "查询成功") : JsonData.buildError("错误");
    }

    @ApiOperation("学生查询自己需要提交的文字作业")
    @GetMapping("/Question_text")
    public JsonData Question_text(@RequestParam("stu_id") String stu_id) {
        List<question_text> target = all_homeworkQuestion.Find_Question_text(stu_id);
        return target.size() != 0 ? JsonData.buildSuccess(target, "查询成功") : JsonData.buildError("错误");
    }
}
